package com.raidroid.rdwhois;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SettingsList extends Activity{	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		ListView settingsListView = (ListView) findViewById(R.id.SettingsView);
		settingsListView.setOnItemClickListener(settingsListOnclickListener);
		
		ArrayList<HashMap<String, String>> settingsList = new ArrayList<HashMap<String, String>>();
		
		HashMap<String, String> columns = new HashMap<String, String>();		
		columns.put("title", getString(R.string.Servers));
		columns.put("desc", getString(R.string.Edit_whois_servers));
		settingsList.add(columns);
		
		columns = new HashMap<String, String>();
		columns.put("title", getString(R.string.Delete_History));
		columns.put("desc", getString(R.string.Delete_History_DSC));
		settingsList.add(columns);
		
		columns = new HashMap<String, String>();
		columns.put("title", getString(R.string.About));
		columns.put("desc", getString(R.string.About_RD_Whois));		
		settingsList.add(columns);
		
		SimpleAdapter mSettings = new SimpleAdapter(this, settingsList, R.layout.settings_row,
	            new String[] {"title", "desc"}, new int[] {R.id.SETTINGS_TITLE, R.id.SETTINGS_DESCRIPTION});		
		settingsListView.setAdapter(mSettings);		
	}
	
    private void DisplayRequest(String title, String str){   	
    	final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.resultdialog);
        dialog.setTitle(title);
        TextView qr = (TextView) dialog.findViewById(R.id.QueryResult);
        qr.setText(str);
        Button DialogClose = (Button) dialog.findViewById(R.id.btnClose);
        DialogClose.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
        Button DialogButtonGoDaddy = (Button) dialog.findViewById(R.id.DialogButtonGoDaddy);
        DialogButtonGoDaddy.setVisibility(View.GONE);
        dialog.show();
    }
    
    private String GetAboutFromTtx(){
    	String str = "";
    	InputStream is = getResources().openRawResource(R.raw.rdwhoisabout);
    	if (is != null) {
    		StringBuilder sb = new StringBuilder();
    		String line;
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    		    while ((line = reader.readLine()) != null) {
    		    	sb.append(line).append("\n");
    		    }} catch (IOException e) {
				} finally {
    		    	try {
						is.close();
					} catch (IOException e) {
					}
    		    }
					PackageInfo info = null;
					try {
						info = getPackageManager().getPackageInfo(getPackageName(), 0);
					} catch (NameNotFoundException e) {}
    		    	str = sb.toString();
    		    	str = str.replace("##versionname##", info.versionName);
    		    }
    	return str;
    }
	
	private OnItemClickListener settingsListOnclickListener = new OnItemClickListener(){
		@Override
		public void onItemClick(AdapterView<?> l, View v, int index,
				long arg3) {			
			switch (index) {
			case 0:
				Intent i = new Intent(SettingsList.this, Servers.class);
	        	startActivity(i);
				break;
			case 1:
				new  AlertDialog.Builder(SettingsList.this)
				.setMessage(R.string.delete_history_alert_message)
				.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						DBAdapter db = new DBAdapter(SettingsList.this);
				    	db.open();
				    	db.deleteAllWords();
				    	db.close();
						dialog.dismiss();
					}
				}).setNeutralButton(R.string.Cancel, new DialogInterface.OnClickListener() {			
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				})
				.setTitle(R.string.Confirm)
				.show(); 
				break;
			case 2:
				DisplayRequest(getString(R.string.About_RD_Whois), GetAboutFromTtx());
				break;
			default:
				break;
			}
		}
	};
}
